from flask import Flask, g
from functools import wraps
from flask_cors import CORS

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        user = g.get('user')
        if user is None:
            return "Not logged in"
        g.set('user', user)
        return f(*args, **kwargs)
    return decorated_function

def create_app():
    app = Flask(__name__, static_url_path=r'')
    CORS(app, supports_credentials=True)
    # 注册蓝图和其他扩展
    from app.admin import admin_bp
    app.register_blueprint(admin_bp)

    from app.captcha import captcha_bp
    app.register_blueprint(captcha_bp)

    from app.chat import chat_bp
    app.register_blueprint(chat_bp)

    from app.function import function_bp
    app.register_blueprint(function_bp)

    
    from app.invite import invite_bp
    app.register_blueprint(invite_bp)

    from app.mj import mj_bp
    app.register_blueprint(mj_bp)

    from app.model import model_bp
    app.register_blueprint(model_bp)
    

    from app.order import order_bp
    app.register_blueprint(order_bp)

    from app.payment import payment_bp
    app.register_blueprint(payment_bp)

    from app.products import products_bp
    app.register_blueprint(products_bp)

    from app.prompt import prompt_bp
    app.register_blueprint(prompt_bp)

    from app.reward import reward_bp
    app.register_blueprint(reward_bp)

    from app.sd import sd_bp
    app.register_blueprint(sd_bp)

    from app.sms import sms_bp
    app.register_blueprint(sms_bp)

    from app.upload import upload_bp
    app.register_blueprint(upload_bp)

    from app.user import user_bp
    app.register_blueprint(user_bp)

    from app.role import role_bp
    app.register_blueprint(role_bp)

    from app.ai import ai_bp
    app.register_blueprint(ai_bp)

    return app
